<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item
          label="字典名称"
          prop="dictName"
          ref="dictName"
          :rules="$valid.required"
          :maxLength="50">
          <a-input v-model="formInfo.dictName" placeholder="请输入字典名称"/>
        </a-form-model-item>

        <a-form-model-item
          label="字典编码"
          prop="dictCode"
          ref="dictCode"
          :rules="$valid.required"
          :maxLength="50">
          <a-input v-model="formInfo.dictCode" placeholder="请输入字典编码" :disabled="visiblekey"/>
        </a-form-model-item>

        <a-form-model-item
          label="描述"
          prop="remark"
          ref="remark"
          :maxLength="499">
          <a-input v-model="formInfo.remark" placeholder="请输入" type="textarea"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { getAction, postAction } from '@/api/manage'

  export default {
    name: 'DictModalEdit',
    data () {
      return {
        visible: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        formInfo: {
          dictName: undefined,
          dictCode: undefined,
          remark: undefined
        },
        url: {
          saveInfo: '/system/sysDict/save',
          getInfo: '/system/sysDict/info'
        },
        visiblekey: false
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({})
      },
      edit (record) {
        if (record.id) {
          this.visiblekey = true
        } else {
          this.visiblekey = false
        }
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
              this.$refs.ruleForm.resetFields()
              this.formInfo = (pick(res.result.data, 'id', 'dictName', 'dictCode', 'remark'))
            })
          })
        }
        this.visible = true
      },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$refs.ruleForm.resetFields()
              this.$message.success(res.content)
              this.visible = false
              this.$emit('ok', this.formInfo)
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close()
      },
      close () {
        this.$refs.ruleForm.resetFields()
        this.$objToReset(this.formInfo)
        this.$emit('close')
        this.visible = false
      }
    }
  }
</script>
